package com.bookcloud.java21.aitools.aie02;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/** Data utility class for generating mock student data 数据工具类，用于生成模拟学生数据 */
public class DataUtil {

  // 存放学生历史签到记录
  private static List<Student> historyList = new ArrayList<>();

  // 今天的学生签到数据
  private static List<Student> todayList = null;

  private static final String[] FIRST_NAMES = {
    "张", "王", "李", "刘", "陈", "杨", "赵", "黄", "周", "吴",
    "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"
  };

  private static final String[] LAST_NAMES = {
    "伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "军",
    "洋", "勇", "艳", "杰", "娟", "涛", "明", "超", "秀兰", "霞",
    "平", "刚", "桂英", "建华", "文", "华", "金凤", "志强", "春梅", "海燕"
  };

  public static List<Student> getAllSignList() {
    // 检测今天的数据在历史的清单是否存在

    // 合并今天学生签到的列表
    historyList.addAll(todayList);
    return historyList;
  }

  /**
   * Generate mock student data list 生成模拟学生数据列表
   *
   * @param count Number of students to generate
   * @return List of Student objects
   */
  public static List<Student> generateMockStudentData(int count) {
    List<Student> students = new ArrayList<>();
    Random random = new Random();
    LocalDate today = LocalDate.now();
    LocalDateTime now = LocalDateTime.now();

    for (int i = 1; i <= count; i++) {
      String name = generateRandomName(random);
      // 获取年月日字符串
      String dateStr = today.toString();
      dateStr = dateStr.replace("-", "");
      String stuNo = dateStr + (100 + i);

      // Random sign-in status (70% chance of being signed in)
      int status = random.nextDouble() < 0.7 ? 1 : 0;

      // 生成签到日期字符串， 格式如：20250807
      String signDate = dateStr;
      // 生成签到时间字符串， 格式如：141223
      String signTime = now.format(java.time.format.DateTimeFormatter.ofPattern("HHmmss"));
      Student student = new Student(name, stuNo, status, signDate, signTime);
      students.add(student);
    }

    return students;
  }

  /** Generate random Chinese name 生成随机中文姓名 */
  private static String generateRandomName(Random random) {
    String firstName = FIRST_NAMES[random.nextInt(FIRST_NAMES.length)];
    String lastName = LAST_NAMES[random.nextInt(LAST_NAMES.length)];

    // Sometimes add a second character to the last name
    if (random.nextDouble() < 0.3) {
      String secondLastName = LAST_NAMES[random.nextInt(LAST_NAMES.length)];
      return firstName + lastName + secondLastName;
    }

    return firstName + lastName;
  }

  /** Get default student data for today's sign-in 获取今日签到的默认学生数据 */
  public static List<Student> getTodaySignData() {
    if (todayList == null) {
      todayList = generateMockStudentData(24);
    }
    return todayList; // Generate 30 students by default
  }

  /** Get students who have signed in today 获取今日已签到的学生 */
  public static List<Student> getSignedInStudents() {
    return getTodaySignData().stream().filter(student -> student.getStatus() == 1).toList();
  }

  /** Get students who haven't signed in today 获取今日未签到的学生 */
  public static List<Student> getNotSignedInStudents() {
    return getTodaySignData().stream().filter(student -> student.getStatus() == 0).toList();
  }
}
